<?php

class PartnerController extends Zend_Controller_Action
{
	private $_auth;
	private $_acl;
    public function init(){
        $acl                = new My_Acl();
        $auth               = $acl->hasIdentity($this->_request);
        $this->_auth        = $auth->getIdentity();
        $this->view->auth   = $this->_auth;
        $this->_acl         = $acl;
        $this->view->acl    = $this->_acl;
        $listAllow          = $acl->listAllowed($this->_auth); 
        $listMenu           = array();
        foreach($listAllow as $row){
            if($row['moduleName'] == 'admin' && $row['actionName'] == 'index' && !in_array($row['controllerName'], $listMenu)){
                $listMenu[]     = $row['controllerName'];
            }
        }
        $this->view->role=$listMenu;
    }
    public function indexAction(){
        
        $modelRole = new Default_Model_Role();
        $roleUser = $modelRole->getDataByUserId($this->_auth['userId'])->toArray();
        $partnerList = array();
        foreach($roleUser as $row){
            if($row['partnerId'])
                $partnerList = array_merge($partnerList, json_decode($row['partnerId']));
        }
        $partnerList = array_unique($partnerList);
        $partnerList = '("'.implode('","',$partnerList).'")';
        $modelPartner = new Default_Model_Partner();
        $partnerList = $modelPartner->getDataByPartnerList($partnerList);
        $this->view->partnerList = $partnerList;
        
    }//end function
     public function deleteAction(){
        if($this->getRequest()->isPost()){
            $del = $this->getRequest()->getPost('del');
            if($del == 'Có'){
              
                $partnerId = $this->_getParam('id');;
                $modelPartner = new Default_Model_Partner();
                $modelPartner->deleteData($partnerId);
            }
            $this->_helper->redirector('index', 'partner', 'default');
        }else{
            $id = $this->_getParam('id', 0);
            $modelPartner = new Default_Model_Partner();
            $this->view->partnerCheck = $modelPartner->getDataById($id);
        }
    }
    public function addAction(){
        $form = new Default_Form_Partner();
        $form->submit->setLabel('Thêm mới');
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $partnerId     = $form->getValue('partnerId');
                //$catId      = $form->getValue('catId');
                $catId      = 0;
                $openDate   = $form->getValue('openDate');
                $partnerName   = $form->getValue('partnerName');
                $status     = $form->getValue('status');
                //$partnerConfig = $form->getValue('partnerConfig');
                
                $upload = new Zend_File_Transfer;
               
                $upload->setDestination(PUBLIC_PATH.'/assets/');
                
                $files = $upload->getFileInfo();
                $upload->addFilter('Rename', date("Y-m-d-H-i-s-").$upload->getFileName(null,false));
                $upload->receive();
                if($upload->getFileName(null,false)){
                    $partnerImg = '/assets/'.$upload->getFileName(null,false);
                }else{
                    $partnerImg = '';
                }
                $modelPartner = new Default_Model_Partner();
                $partnerId = $modelPartner->addData($partnerId, $partnerName, $partnerImg, $this->_helper->time->dateToData($openDate), $this->_auth['userId'],date("Y-m-d"), $status);
                $this->_helper->redirector('index', 'partner', 'default');

            }else{
                $form->populate($formData);
            }
        }
    }
    public function editAction(){
		$form = new Default_Form_Partner();
		$form->submit->setLabel('Lưu');
		$this->view->form = $form;
		$id = $this->_getParam('id', 0);
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();            
			if($form->isValid($formData)){
                $modelPartner = new Default_Model_Partner();
    			$partnerCheck = $modelPartner->getDataByID($id);
                    
                $partnerId     = $form->getValue('partnerId');
                $openDate   = $form->getValue('openDate');
                $partnerName   = $form->getValue('partnerName');
                $status     = $form->getValue('status');
                //$partnerConfig = $form->getValue('partnerConfig');
                
                $repair     = $this->_getParam('resourceId');

                $upload = new Zend_File_Transfer;
                $upload->setDestination(PUBLIC_PATH.'/assets/');
                $files = $upload->getFileInfo();
                $upload->addFilter('Rename', date("Y-m-d-H-i-s-").$upload->getFileName(null,false));
                $upload->receive();
                if($upload->getFileName(null,false)){
                    $partnerImg = '/assets/'.$upload->getFileName(null,false);
                    unlink(ROOT_PATH."/public".$partnerCheck['partnerImg']);
                }else{
                    $partnerImg = $partnerCheck['partnerImg'];
                }
                $modelPartner = new Default_Model_Partner();
                $partnerCheck = $modelPartner->getDataByPartnerName($partnerName);
                
                if(!$partnerCheck || $partnerCheck['partnerId'] == $partnerId){
                    $modelPartner->updateData($id, $partnerName,  $partnerImg, $this->_helper->time->dateToData($openDate), json_encode($repair), $status);

                    $this->_helper->redirector('index', 'partner', 'default');
                }else{
                    $this->view->mess ="Tên partner đã tồn tại!!!";
                    $this->view->flag = "alert";
    				$form->populate($formData);
    			}
			}else{
				$form->populate($formData);
			}
		}else{
            $modelPartner = new Default_Model_Partner();
			$partnerCheck = $modelPartner->getDataByID($id)->toArray();
            if($partnerCheck){

                
    			$this->view->partnerCheck = $partnerCheck;
                if(isset($partnerCheck['openDate'])){
                    $partnerCheck['openDate'] = $this->_helper->time->dataToDate($partnerCheck['openDate']);
                }
    			$form->populate($partnerCheck);
                                
               
            }else{
                $this->_helper->redirector('index', 'partner', 'default');
            }
		}
    }
    
    
    
}//end class